class EventController {
  def babyService
  def index = { }
  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    [eventList: Event.list(params), total: Event.count()]
  }

  def recent = {
    def baby = Baby.get(params.id)
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    params.sort = params.sort ? params.sort : 'dateCreated'
    params.order = params.order ? params.order : 'desc'
    def total = Event.countByBaby(baby)
    [eventList: Event.findAllByBaby(baby, params), total:total]
  }

  def stage = {
     def baby = Baby.get(params.id)
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    params.sort = params.sort ? params.sort : 'dateCreated'
    params.order = params.order ? params.order : 'desc'
    def stage = babyService.getStage(baby)
    def sql = "from Event as b where b.baby.birth > :startStage and b.baby.birth < :endStage"
    def total = Event.executeQuery("select count(*) " + sql, [startStage:stage - 30, endStage:stage + 30])[0].toInteger()
    [eventList: Event.findAll(sql, [startStage:stage - 30, endStage:stage + 30], params), total: total]
  }
  

}
